package com.pc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pc.entity.Orderstate;
import com.pc.mapper.OrderstateMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderStateService {
    @Resource
    private OrderstateMapper orderstateMapper;

    //添加记录
    public boolean addOrderState(Orderstate orderstate){
        int insert = orderstateMapper.insert(orderstate);
        return insert!=0;
    }
    //更改订单状态
    public boolean updateState(String username){
        QueryWrapper<Orderstate> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);

        List<Orderstate> list = orderstateMapper.selectList(wrapper);

        if(list.size()>0){
            Orderstate result = list.get(list.size() - 1);
            QueryWrapper<Orderstate> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("createdate",result.getCreatedate());
            String s = result.getState();

            if(s.equals("顾客下单"))
                result.setState("商家接单");
            else if(s.equals("商家接单"))
                result.setState("顾客付款");
            int update = orderstateMapper.update(result, wrapper1);
            return update!=0;
        }
        //当前表中没有这个记录
       return false;

    }

    //获得最近订单状态
    public String getState(String username){
        QueryWrapper<Orderstate> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);

        List<Orderstate> list = orderstateMapper.selectList(wrapper);

        if(list.size()>0){
            Orderstate result = list.get(list.size() - 1);
            return result.getState();
        }else{
            return "尚未添加";
        }


    }

    //获得已完成订单
    public List<Orderstate> getFinshOrder(String username){
        QueryWrapper<Orderstate> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username).eq("state","顾客付款");
        return orderstateMapper.selectList(wrapper);
    }

    /**
     * 查询表中已支付的订单根据年月日返回数
     */
    public int[] CountOrderFinshed( ){

        QueryWrapper<Orderstate> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("state","顾客付款");
        List<Orderstate> orderstates1 = orderstateMapper.selectList(wrapper1);

        //时间
        Date date1 = new Date(); SimpleDateFormat dateFormat= new
                SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String stringdate = dateFormat.format(date1);
        String nowyear=stringdate.substring(0,4);
        String nowmonth=stringdate.substring(5,7);
        String nowday=stringdate.substring(0,10);
        //现在的年月日nowyear,nowmonth,nowday
        int countyear=0;//计数年
        int countmonth=0;//计数月
        int countday=0;//计数日
        for(Orderstate a:orderstates1){
            //表中的年月日  createdateyear,createdatemonth,createdateday
            String createdateday=a.getCreatedate().substring(0,10);
            String createdatemonth=a.getCreatedate().substring(5,7);
            String createdateyear=a.getCreatedate().substring(0,4);

            if(createdateyear.equals(nowyear))
                countyear++;
            if(createdatemonth.equals(nowmonth))
                countmonth++;
            if(createdateday.equals(nowday))
                countday++;
        }

        int[] d =new int[3];
        d[0]=countyear;
        d[1]=countmonth;
        d[2]=countday;

        return d;
    }

    /**
     * 今天订单情况状况
     * 分为顾客下单, 商家接单.已完成
     */
    public int[] CountOrderState(){
        int[] state =new int[3];

        Date date1 = new Date(); SimpleDateFormat dateFormat= new
                SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String stringdate = dateFormat.format(date1);
        String nowDay     = stringdate.substring(0,10);

        QueryWrapper<Orderstate> wrapper = new QueryWrapper<>();
        List<Orderstate> list = orderstateMapper.selectList(wrapper);
        for (Orderstate a:list){
            String createdate = a.getCreatedate();
            String tablenowday = createdate.substring(0, 10);
            if(nowDay.equals(tablenowday) && a.getState().equals("顾客下单")){
                state[0]++;
            }else if(nowDay.equals(tablenowday) && a.getState().equals("商家接单")){
                state[1]++;
            }else if(nowDay.equals(tablenowday) && a.getState().equals("顾客付款")){
                state[2]++;
            }
        }


        return state;
    }

    /**
     * 获得每个月的订单数
     */
    public int[] getMonthOrderFinsh(){
        int[] month =new int[12];
        QueryWrapper<Orderstate> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("state","顾客付款");
        List<Orderstate> orderstates1 = orderstateMapper.selectList(wrapper1);
        //时间
        for(Orderstate a:orderstates1){
            String createdatemonth=a.getCreatedate().substring(5,7);
            switch (createdatemonth) {
                case "01":
                    month[0]++;
                    break;
                case "02":
                    month[1]++;
                    break;
                case "03":
                    month[2]++;
                    break;
                case "04":
                    month[3]++;
                    break;
                case "05":
                    month[4]++;
                    break;
                case "06":
                    month[5]++;
                    break;
                case "07":
                    month[6]++;
                    break;
                case "08":
                    month[7]++;
                    break;
                case "09":
                    month[8]++;
                    break;
                case "10":
                    month[9]++;
                    break;
                case "11":
                    month[10]++;
                    break;
                case "12":
                    month[11]++;
                    break;
            }
        }
        return month;

    }

    /**
     * 获得点单状态
     */
    public List<Orderstate> exportExcelOrderState(){
        QueryWrapper<Orderstate> wrapper = new QueryWrapper<>();
        wrapper.eq("state","顾客付款");
        List<Orderstate> orderstates = orderstateMapper.selectList(wrapper);
        return orderstates;
    }
}
